﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using VitCode.ObjectResolutionHelper;

namespace VitCode.Castle.Entities
{
    [OrMemo("用户组")]
    public class UserGroup:BaseEntity
    {
        protected string m_Name;
        /// <summary>
        /// 名稱
        /// </summary>
        [OrMemo("名称")]
        public virtual string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }

        protected string m_TypeName;
        /// <summary>
        /// 類型名稱
        /// </summary>
        [OrMemo("类型名称")]
        public virtual string TypeName
        {
            get { return m_TypeName; }
            set{ m_TypeName = value;}
        }


        protected UserGroup m_Parent;
        /// <summary>
        /// 父功能表
        /// </summary>
        [OrMemo("父节点")]
        public virtual UserGroup Parent
        {
            get { return m_Parent; }
            set
            {
                if (value != null && value == this)
                    throw new ArgumentException("不是設置本節點為父節點");
                m_Parent = value;
            }
        }

        protected string m_OrderId = "";
        /// <summary>
        /// 節點樹模型的層次資訊
        /// </summary>
        [OrMemo("OrderId")]
        public virtual string OrderId
        {
            get { return m_OrderId; }
            set
            {
                m_OrderId = m_Id.ToString();
                if (m_OrderId == null || m_OrderId.Length < m_Id.ToString().Length)
                    throw new ArgumentException("OrderId節點資訊有誤");
                if (m_Parent != null)
                    m_OrderId = m_Parent.OrderId + "." + m_Id;
                else
                    m_OrderId = m_Id.ToString();
                m_OrderId = value;
            }
        }

        /// <summary>
        /// 重寫Equals方法
        /// </summary>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(obj, this))
                return true;
            if (obj == null)
                return false;
            var sObj = obj as UserGroup;
            return sObj != null && (sObj.Id == m_Id || (sObj.Name == m_Name));
        }

        /// <summary>
        /// 重寫GetHashCode方法
        /// </summary>
        public override int GetHashCode()
        {
            int hash = 55;
            hash = 27 * hash * m_Id.GetHashCode();
            return hash;
        }
    }
}
